<?php

namespace App\Admin\Controllers\education;

use App\Admin\Renderable\UserTable;
use App\Http\Logic\RegionLogic;
use App\Models\User;
use App\Models\AdminSchool;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class SchoolController extends AdminController
{
    protected $title = "学校";

    protected $translation = 'admin-school';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(AdminSchool::with('user'), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('school_name');
            $grid->column('user.name','联系人');
            $grid->column('user.phone','联系电话');
            $grid->column('address')->display(function () {
                $province = RegionLogic::getName($this->province);
                $city = RegionLogic::getName($this->city);
                $district = RegionLogic::getName($this->district);
                return $province.' '.$city.' '.$district.' '.$this->address;
            });
            $grid->column('school_type') -> using(trans('admin-school.options.school_type'));

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('school_name') -> width(3);
                $filter->equal('school_type') -> width(3) ->select(trans('admin-school.options.school_type'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdminSchool(), function (Show $show) {
            $show->field('id');
            $show->field('school_name');
            $show->field('users_id');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('address');
            $show->field('school_type');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminSchool(), function (Form $form) {
            $form->display('id');


            $form->row(
                function (Form\Row $form) {
                    $form->text('school_name')->required();
                    $form->selectTable('user_id','联系人')
                        ->from(UserTable::make(['id' => $form->getKey()]))
                        ->model(User::class, 'id', 'name')
                        ->required();

                    $form->width(4)->select('province')
                        ->options(
                            RegionLogic::getProvinces()
                        )
                        ->load('city', '/region/cities'); // 选中省后加载市

                    $form->width(4)->select('city')
                        ->options(function ($provinceId) {
                            return RegionLogic::getCities($provinceId);
                        })
                        ->load('district', '/region/districts'); // 选中市后加载区

                    $form->width(4)->select('district')
                        ->options(function ($cityId) {
                            return RegionLogic::getDistricts($cityId);
                         });

                    $form->text('address');

                    $form->select('school_type') ->options(function () {
                        return trans('admin-school.options.school_type');
                    });
                }
            );





            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
